﻿using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CubicFWNet.Application.SystemManager
{
    public class ItemApp
    {
        private readonly ItemRepository _itemRepository = new ItemRepository();
        private readonly ItemCategoryRepository _itemCategoryRepository = new ItemCategoryRepository();


        public IEnumerable<Item> GetItems(Guid categoryId)
        {
            return _itemRepository.IQueryable(a => a.CategoryId == categoryId || categoryId == Guid.Empty).OrderBy(a=>a.SortNo);
        }
        public Item GetItem(Guid itemId)
        {
            return _itemRepository.FindEntity(itemId);
        }


        public int Delete(Guid id)
        {
            var del = _itemRepository.FindEntity(u => u.Id == id);
            if (del == null) return -404;
            return _itemRepository.Delete(u => u.Id == id);
        }
        public int AddOrUpdate(Item item)
        {
            if (item.Id == Guid.Empty)
            {
                item.Id = Guid.NewGuid();
                return _itemRepository.Insert(item);
            }
            else
            {
                return _itemRepository.Update(item);
            }
        }



        public IEnumerable<ItemCategory> GetItemCategory()
        {
            return _itemCategoryRepository.IQueryable();
        }



    }
}
